import predict
import models
import tool
import setting
import os


# 每隔一个小时就保存一次模型测试结果
# sleeptime = tool.sleeptime(1)
# tool.whileFn(predict.testPredict, sleeptime)

def train():
  trainList = [
    # [20, 8, 16, 0.3, 16, 0.1],  # 3000:5s
    # [20, 32, 16, 0.3, 16, 0.1], # 3000:3
    [20, 8, 64, 0.3, 16, 0.1], # 3000:18
    [20, 32, 64, 0.3, 16, 0.1], # 3000:5

    # [20, 8, 16, 0.3, 64, 0.1], # 3000:4
    # [20, 32, 16, 0.3, 64, 0.1], # 3000:2
    [20, 8, 64, 0.3, 64, 0.1], # 3000:20
    [20, 32, 64, 0.3, 64, 0.1], # 3000:8
  ]
  set = setting.Setting()
  for i in range(0, len(trainList)):
    item = trainList[i]
    set.setDef(
      EPOCHS_=item[0],
      BATCH_SIZE_=item[1],
      MAX_STEPS_=item[2],
      DROPOUT_RATE_=item[3],
      LSTM_UNITS_=item[4],
      REGULARIZERS_=item[5],
      CHECKPOINTS_PATH_= "predictModel_1000",
      ExeQuery_="10000,1000")
    num = "a0000"
    if i < 10:
      num = num + "{}_".format(i)
    elif i < 100:
      num = num[:-1] + "{}_".format(i)
    else:
      num = num[:-2] + "{}_".format(i)
    try:
      models.trainModel(set,num)
    except Exception as e:
      print(e)


def testPredict(limitStart=13000, limitEnd=200):
  set = setting.Setting()
  set.setDef(
    CHECKPOINTS_PATH_= "predictModel_500",
    SavaDataFile_= "train_500.json",
    ExeQuery_="{},{}".format(limitStart, limitEnd))
  # 获取文件夹地址
  path = os.path.split(os.path.realpath(__file__))[0]
  getFilesList = predict.getFiles(path + "/" + set.CHECKPOINTS_PATH)
  # getFilesList = ["a00317__60_2_16_0.6_16.h5"]
  for item in range(0, len(getFilesList)):
    item = getFilesList[item]
    if item.split(".")[-1:][0] == "png":
      continue
    item = item[:-3]
    sl = item.split("_")
    s = []
    for _ in sl:
      if _: s.append(_)
    sl = s
    sl[0] = sl[0] + "_"
    for i in range(1, len(sl)):
      if i == 4:
        sl[i] = float(sl[i])
        continue
      if i == 6:
        sl[i] = float(sl[i])
        continue
      sl[i] = int(sl[i])
    set.setDef(EPOCHS_=sl[1], BATCH_SIZE_=sl[2], MAX_STEPS_=sl[3], DROPOUT_RATE_=sl[4], LSTM_UNITS_=sl[5])
    predict.trainPredict_(set,sl[0],sl[1],sl[2],sl[3],sl[4],sl[5],sl[6])
  print("======================================================================================")
  print("=================================    验证完成    =====================================")

def train1():
  start = 13000
  end = 200
  cs = 30
  for i in range(13000, end*cs+start, 200):
    print(i)
    testPredict(i, 200)


def train2():
  # 没训练一次就验证测试
  start = 34880
  end = 60
  trainMax = 5000 # 训练数据大小
  cs = 300
  for init in range(start, end*cs+start, end):
    print("==============================")
    print("=======当前开始值为 {} =======".format(init))
    print("==============================")
    print(init)
    
    # 训练
    trainList = [
      [5, 8, 16, 0.3, 16, 0.1],  # 3000:5s
      [5, 32, 16, 0.3, 16, 0.1], # 3000:3
      # [20, 8, 64, 0.3, 16, 0.1], # 3000:18  // 发送错误
      # [20, 32, 64, 0.3, 16, 0.1], # 3000:5  // 发送错误

      [5, 8, 16, 0.3, 64, 0.1], # 3000:4
      [5, 32, 16, 0.3, 64, 0.1], # 3000:2
      # [20, 8, 64, 0.3, 64, 0.1], # 3000:20  // 发送错误
      # [20, 32, 64, 0.3, 64, 0.1], # 3000:8  // 发送错误

      [5, 4, 8, 0.3, 64, 0.01],
      [5, 4, 8, 0.3, 16, 0.01],

      # [5, 8, 64, 0.3, 64, 0.01],  // 发送错误
      # [5, 8, 64, 0.3, 16, 0.01],  // 发送错误
    ]
    set = setting.Setting()
    for i in range(0, len(trainList)):
      item = trainList[i]
      set.setDef(
        EPOCHS_=item[0],
        BATCH_SIZE_=item[1],
        MAX_STEPS_=item[2],
        DROPOUT_RATE_=item[3],
        LSTM_UNITS_=item[4],
        REGULARIZERS_=item[5],
        CHECKPOINTS_PATH_= "predictModel__",
        ExeQuery_="{},{}".format(init, trainMax))
      print("训练数据为：{},{}".format(init, trainMax))
      num = "a0000"
      if i < 10:
        num = num + "{}_".format(i)
      elif i < 100:
        num = num[:-1] + "{}_".format(i)
      else:
        num = num[:-2] + "{}_".format(i)
      try:
        print("开始训练")
        models.trainModel(set,num)
      except Exception as e:
        print(e)
      
      # 验证
      try:
        set.setDef(
          SavaDataFile_= "train__.json",
          ExeQuery_="{},{}".format(init+trainMax, end))
        print("开始验证==========================")
        print("验证数据为：{},{}".format(init+trainMax, end))
        predict.trainPredict_(set,num,item[0],item[1],item[2],item[3],item[4],item[5])
        print("===================================================")
        print("=================    验证完成    ===============")
      except Exception as e:
        print('{}_{}_{}_{}_{}_{}_{}.h5'.format(num,item[0],item[1],item[2],item[3],item[4],item[5]))
        print("发送错误===========================")
        print("发送错误===========================")
        print(e)
      
  
    # # 验证
    # set = setting.Setting()
    # set.setDef(
    #   CHECKPOINTS_PATH_= "predictModel__",
    #   SavaDataFile_= "train__.json",
    #   ExeQuery_="{},{}".format(init+trainMax, end))
    # print("验证数据为：{},{}".format(init+trainMax, end))
    # # 获取文件夹地址
    # path = os.path.split(os.path.realpath(__file__))[0]
    # getFilesList = predict.getFiles(path + "/" + set.CHECKPOINTS_PATH)
    # # getFilesList = ["a00317__60_2_16_0.6_16.h5"]
    # for item in range(0, len(getFilesList)):
    #   item = getFilesList[item]
    #   item = item[:-3]
    #   sl = item.split("_")
    #   s = []
    #   for _ in sl:
    #     if _: s.append(_)
    #   sl = s
    #   sl[0] = sl[0] + "_"
    #   for i in range(1, len(sl)):
    #     if i == 4:
    #       sl[i] = float(sl[i])
    #       continue
    #     if i == 6:
    #       sl[i] = float(sl[i])
    #       continue
    #     sl[i] = int(sl[i])
    #   set.setDef(EPOCHS_=sl[1], BATCH_SIZE_=sl[2], MAX_STEPS_=sl[3], DROPOUT_RATE_=sl[4], LSTM_UNITS_=sl[5])
    #   try:
    #     print("开始验证==========================")
    #     predict.trainPredict_(set,sl[0],sl[1],sl[2],sl[3],sl[4],sl[5],sl[6])
    #   except Exception as e:
    #     print('{}_{}_{}_{}_{}_{}_{}.h5'.format(sl[0],sl[1],sl[2],sl[3],sl[4],sl[5],sl[6]))
    #     print("发送错误===========================")
    #     print("发送错误===========================")
    #     print(e)
    # print("======================================================================================")
    # print("=================================    验证完成    =====================================")

def authenticPredict():
  print("")

if __name__ == '__main__':
  # train()
  # train1()
  train2()
  print("==============================================")
